package com.cindata

import com.cindata.common.Result
import com.cindata.dic.DictoryTypeDict
import com.cindata.dimension.CustomDimension
import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
class CustomerDimensionController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    /**
     * 首页数据回显
     * @param max
     * @return
     */
    def index(Integer max)
    {
        def username = springSecurityService.getPrincipal().username
        def user = User.findByUsername(username)

        def dimension = DictoryTypeDict.EstateRisk.getName().toString()

        //获取类型
        def dictoryMap = [:]
        dictoryMap.put(DictoryTypeDict.EstateRisk.getName(), DictoryTypeDict.EstateRisk.getLabel())
        dictoryMap.put(DictoryTypeDict.ReportScope.getName(), DictoryTypeDict.ReportScope.getLabel())


        //查询一级参数
        def primaryDimensionList = [:]
        if (null == params['id'] && 'null'.equals(params['id'].toString()))
        {
            def estateRiskDimensionList = CustomDimension.findAllByDictoryTypeAndParentIsNull(DictoryTypeDict.EstateRisk.getName())
            primaryDimensionList.put(DictoryTypeDict.EstateRisk.getName(), estateRiskDimensionList)
        }
        else
        {
            def estateRiskDimensionList = CustomDimension.findAllByDictoryTypeAndParentIsNull(params['id'])
            primaryDimensionList.put(params['id'], estateRiskDimensionList)
        }

        //查询二级参数
        def secondaryDimensionList = CustomDimension.findAllByParentIsNotNull()
        //获取页面选择类型
        if (params['id'])
        {
            dimension = params['id'].toString()
        }
        respond user, model: [primaryDimensionList: primaryDimensionList, secondaryDimensionList: secondaryDimensionList, dictoryMap: dictoryMap, dimension: dimension]
    }

    /**
     * 新增一级参数*/
    @Transactional
    def createPrimaryDimension()
    {

        Result result = new Result(false)

        try
        {

            if (params)
            {
                CustomDimension customDimension = new CustomDimension()

                if (CustomDimension.findAllByLabel(params['label'].toString()) || CustomDimension.findAllByCode(params['code'].toString()))
                {
                    result.success = false
                }
                else
                {
                    customDimension.label = params['label'].toString()
                    customDimension.code = params['code'].toString()
                    if (params['dictoryType'].toString().equals(DictoryTypeDict.EstateRisk.getName().toString()))
                    {
                        customDimension.dictoryType = DictoryTypeDict.EstateRisk
                    }
                    else
                    {
                        customDimension.dictoryType = DictoryTypeDict.ReportScope
                    }
                    customDimension.orderNumber = Integer.parseInt(params['orderNumber'])
                    customDimension.save()
                    result.success = true
                }
            }
            else
            {
                result.success = false
                result.message = "值不能为空！"
            }

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON
    }

    /**
     * 新增二级参数*/
    @Transactional
    def createSecondaryDimension()
    {

        Result result = new Result(false)

        try
        {

            if (params)
            {
                CustomDimension customDimension = new CustomDimension()

                if (CustomDimension.findAllByLabel(params['label'].toString()))
                {
                    result.success = false
                }
                else
                {
                    customDimension.parent = CustomDimension.findById(Long.parseLong(params['id']))
                    customDimension.label = params['label'].toString()
                    customDimension.orderNumber = Integer.parseInt(params['orderNumber'])
                    customDimension.minValue = Float.parseFloat(params['minValue'])
                    customDimension.maxValue = Float.parseFloat(params['maxValue'])

                    if (params['minOperator'].toString().equals("1"))
                    {
                        customDimension.minOperator = ">"
                    }
                    else if (params['minOperator'].toString().equals("2"))
                    {
                        customDimension.minOperator = ">="
                    }
                    else if (params['minOperator'].toString().equals("3"))
                    {
                        customDimension.minOperator = "="
                    }
                    else if (params['minOperator'].toString().equals("4"))
                    {
                        customDimension.minOperator = "<"
                    }
                    else if (params['minOperator'].toString().equals("5"))
                    {
                        customDimension.minOperator = "<="
                    }

                    if (params['maxOperator'].toString().equals("1"))
                    {
                        customDimension.maxOperator = ">"
                    }
                    else if (params['maxOperator'].toString().equals("2"))
                    {
                        customDimension.maxOperator = ">="
                    }
                    else if (params['maxOperator'].toString().equals("3"))
                    {
                        customDimension.maxOperator = "="
                    }
                    else if (params['maxOperator'].toString().equals("4"))
                    {
                        customDimension.maxOperator = "<"
                    }
                    else if (params['maxOperator'].toString().equals("5"))
                    {
                        customDimension.maxOperator = "<="
                    }
                    customDimension.define = params['define'].toString()
                    customDimension.save()
                    result.success = true
                }

            }
            else
            {
                result.success = false
                result.message = "值不能为空！"
            }

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON
    }

    /**
     * 删除一级参数*/
    @Transactional
    def removePrimaryDimension()
    {
        Result result = new Result(false)

        try
        {
            def customDimension = CustomDimension.findById(Long.parseLong(params['id']))
            def customDimensionList = CustomDimension.findAllByParent(customDimension)
            customDimensionList.each {
                it.delete()
            }
            customDimension.delete()
            result.success = true

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON
    }

    /**
     * 删除二级参数*/
    @Transactional
    def removeSecondaryDimension()
    {
        Result result = new Result(false)

        try
        {

            def customDimension = CustomDimension.findById(Long.parseLong(params['id']))
            customDimension.delete()
            result.success = true

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON
    }

    /**
     * 修改一级参数*/
    @Transactional
    def modifyPrimaryDimension()
    {
        Result result = new Result(false)

        try
        {

            def id = Long.parseLong(params['id'])
            if (params)
            {

                CustomDimension customDimension = CustomDimension.findById(Long.parseLong(params['id']))
                customDimension.label = params['label'].toString()
                customDimension.orderNumber = Integer.parseInt(params['orderNumber'])
                customDimension.code = params['code'].toString()
                customDimension.save()
                result.success = true
            }
            else
            {
                result.success = false
                result.message = "值不能为空！"
            }

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON

    }

    /**
     * 修改二级参数*/
    @Transactional
    def modifySecondaryDimension()
    {

        Result result = new Result(false)

        try
        {

            if (params)
            {
                CustomDimension customDimension = CustomDimension.findById(Long.parseLong(params['id']))
                customDimension.label = params['label'].toString()
                customDimension.orderNumber = Integer.parseInt(params['orderNumber'])
                customDimension.minValue = Float.parseFloat(params['minValue'])
                customDimension.maxValue = Float.parseFloat(params['maxValue'])

                if (params['minOperator'].toString().equals("1"))
                {
                    customDimension.minOperator = ">"
                }
                else if (params['minOperator'].toString().equals("2"))
                {
                    customDimension.minOperator = ">="
                }
                else if (params['minOperator'].toString().equals("3"))
                {
                    customDimension.minOperator = "="
                }
                else if (params['minOperator'].toString().equals("4"))
                {
                    customDimension.minOperator = "<"
                }
                else if (params['minOperator'].toString().equals("5"))
                {
                    customDimension.minOperator = "<="
                }

                if (params['maxOperator'].toString().equals("1"))
                {
                    customDimension.maxOperator = ">"
                }
                else if (params['maxOperator'].toString().equals("2"))
                {
                    customDimension.maxOperator = ">="
                }
                else if (params['maxOperator'].toString().equals("3"))
                {
                    customDimension.maxOperator = "="
                }
                else if (params['maxOperator'].toString().equals("4"))
                {
                    customDimension.maxOperator = "<"
                }
                else if (params['maxOperator'].toString().equals("5"))
                {
                    customDimension.maxOperator = "<="
                }
                customDimension.define = params['define'].toString()
                customDimension.save()
                result.success = true
            }
            else
            {
                result.success = false
                result.message = "值不能为空！"
            }

        }
        catch (Exception e)
        {
            result.success = false
            e.printStackTrace()
        }

        render result as JSON
    }

}
